<?php
namespace Biz\model;

use Doraemon\pockets\datebase\BaseDbModel;

class ShopServiceStatement extends BaseDbModel
{

	const _CONFIG_FILE_ = 'db_shop';

	const _CONFIG_SELECT_ = 'db_shop';

	const _DATABASE_ = 'shop';

	const _TABLE_NAME = 'shop_service_statement';

	const _SUB_TABLE = false;


	const STATEMENT_STATUS_NORMAL = 1;
	const STATEMENT_STATUS_DEL = 0;


	public static function getList($shopIds)
	{
		$infos = self::select('*', [
			'shop_id' => $shopIds,
			'statement_status' => self::STATEMENT_STATUS_NORMAL
		]);

		$results = [];
		foreach ($infos as $k => $info) {
			$results[$info['shop_id']][$info['category_id']] = $info;
			$results[$info['shop_id']][$info['category_id']]['service_statement'] =  explode(',',$info['service_statement']);
			sort($results[$info['shop_id']][$info['category_id']]['service_statement']);
		}
		return $results;
	}

	public static function saveStatement($shopid, $cateId, $data)
	{
		$data['service_statement'] = implode(',', $data['service_statement']);
		if (self::get('*', ['shop_id' => $shopid, 'category_id'=>$cateId])) {
			$data['statement_status'] = self::STATEMENT_STATUS_NORMAL;
			return self::update($data, [
				'shop_id' => $shopid,
				'category_id' => $cateId
			]) -> rowCount();
		}

		self::insert([
			'shop_id' =>$shopid,
			'category_id' =>$cateId,
			'service_statement' => $data['service_statement'],
			'statement_status' =>self::STATEMENT_STATUS_NORMAL,
		]);

		return self::id();
	}

}